﻿@model IQueryable<DNA.Mvc.PrivateMessage>
@section HeaderSection {
    <style type="text/css">
        .d-msgs { margin: 0px; padding: 0px; color: #777; text-shadow: 1px 1px white; }
        .d-msgs > li { list-style: none; clear: left; padding: 5px; min-height: 90px; display: inline-block; }
        .d-msgs .d-sender { float: left; width: 60px; text-align: center; }
        .d-msgs > li .d-photo { background: #EFEFEF; padding: 5px; text-transform: lowercase; }
        .d-msgs > li .d-photo > img { width: 48px; height: 48px; }
        .d-msgs > li > section { float: left; padding: 0px; min-height: 80px; width: 600px; border: 1px solid #efefef; padding: 10px; }
        .d-triangle-left { width: 10px; height: 10px; margin-top: 5px; border-color: transparent #efefef transparent transparent; float: left; border-style: solid; border-width: 5px 10px 5px 10px; width: 0; height: 0; }
        .d-triangle-right { width: 10px; height: 10px; margin-top: 15px; border-color: transparent transparent transparent #efefef; float: right; border-style: solid; border-width: 5px 10px 5px 10px; width: 0; height: 0; }
        .d-msgs > li > section > .d-msg-body { padding-top: 5px; font-size: 120%; clear: left; color: #6d6d6d; height: 25px; overflow: hidden; -moz-transition: all 0.2s ease-out; -o-transition: all 0.2s ease-out; -webkit-transition: all 0.2s ease-out; transition: all 0.2s ease-out; }
        .d-msgs > li > section > .d-msg-body-selected { height: auto; }
        .d-msgs .d-horizontal-list a, .d-msgs .d-horizontal-list li { color: #6d6d6d; }
    </style>
}
@UIHelper.ImageTitle(Html.Global("RES_MYMSGS"), "", Url.Content("~/content/images/mail_lock.png"))
<p>
    @Ajax.Dna().Button("btnSendMsg").LocText("RES_NEW").Click("_writeMsg();").ImageIcons(Url.Content("~/content/images/mail.png")).GetHtml()
</p>
@if (Model.Count() > 0)
{
    <ul class="d-msgs">
        @foreach (var pm in Model)
        {
            var IsMe = false;
            if (pm.Sender.Equals(User.Identity.Name))
            {
                IsMe = true;
            }
            <li id="msg_@(pm.ID)" data-sender="@pm.Sender" data-subject="@(pm.Title.StartsWith("RE:") ? pm.Title : "RE:" + pm.Title)">
                <div class="d-sender" @if (IsMe)
                                      {<text> style="float:right;" </text>}>
                    <div class="d-photo ui-corner-all">
                        <img src="@Membership.GetUser(pm.Sender).GetAvatar(48)" alt="@Membership.GetUser(pm.Sender).GetDisplayName()" />
                    </div>
                    @Html.UserLink(pm.Sender)
                </div>
                @if (!IsMe)
                {
                    <div class="d-triangle-left">
                    </div>}
                else
                { 
                    <div class="d-triangle-right">
                    </div>
                }
                <section class="ui-corner-all" @if (IsMe)
                                               {<text> style="float:right;background:#efefef;" </text>}>
                    <header>@pm.Title
                    </header>
                    <div class="d-msg-body">@TextEngine.SafeHtml(pm.Content)</div>
                    <footer>
                        <ul class="d-horizontal-list">
                            <li>@Html.Global("RES_RECEIVER") : @pm.Receivers</li>
                            <li>@Ajax.Dna().Button("btnPostDate" + pm.ID.ToString()).ChangeType(ButtonTypes.LinkButton).ImageIcons(Url.Content("~/content/images/calendar_empty_16.png")).Text(Html.Global(pm.Sent)).GetHtml()</li>
                            @if (!IsMe)
                            {
                                <li>@Ajax.Dna().Button("btnReply" + pm.ID.ToString()).ChangeType(ButtonTypes.LinkButton).ImageIcons(Url.Content("~/content/images/mail_send_16.png")).Text(Html.Global("forums", "Reply")).Click("_replyMsg(" + pm.ID.ToString() + ");").GetHtml()</li>
                            }
                            <li>@Ajax.Dna().Button("btnDel" + pm.ID.ToString()).ChangeType(ButtonTypes.LinkButton).ImageIcons(Url.Content("~/content/images/icon_delete_16.png")).Text(Html.Global("RES_DELETE")).Click("_delMsg(" + pm.ID.ToString() + ");").GetHtml()</li>
                        </ul>
                    </footer>
                </section>
            </li>
        }
    </ul>
        Ajax.Dna().Pager().Load(Url.Action("Inbox", "Account", new { Area = "" })).Render();
}
else
{ 
    <div style="padding: 20px;">
        No message yet.</div>
}
<div id="MailDlg">
    <form id="msgForm" action="@Url.Action("SendMsg", "Account", new RouteValueDictionary(new { Area = "" }))" method="post">
    @Html.AntiForgeryToken()
    <table style="background-color: #ffffff; color: #000000;" cellpadding="5" cellspacing="0">
        <tr>
            <td align="right">
                @Html.Global("RES_SUBJECT"):
            </td>
            <td>
                @Ajax.Dna().TextBox("txtSubject").Width(300).GetHtml()
            </td>
        </tr>
        <tr>
            <td align="right">
                @Html.Global("RES_RECEIVER"):
            </td>
            <td>
                @Ajax.Dna().TextBox("txtReceiver").Width(300).GetHtml()
            </td>
        </tr>
        <tr>
            <td colspan="2">
                @Ajax.Dna().TextArea("txtContent").Width(475).Height(200).GetHtml()
                @*    @{ Ajax.Dna().RichTextBox("txtContent").AllowHTMLEditing(false).ToolPanes(panes => { panes.Clear(); panes.Add().AddSimpleFontTools(); }).Render(); }*@
            </td>
        </tr>
    </table>
    </form>
</div>
@Html.StartupScripts()
<script type="text/javascript">
    function _writeMsg(){
       $("#txtReceiver").val("");  
       $("#txtContent").val("");
       $('#MailDlg').dialog("open");
        $("#txtSubject").val("").focus();
    }

    function _sendMsg() {
        var data = {
            "__RequestVerificationToken": $("#msgForm>input").val(),
            "Subject": $("#txtSubject").val(),
            "To": $("#txtReceiver").val(),
            "Content": $("#txtContent").val()
            // $("#txtContent").richtextbox("getHtml")
        };
        $.post($("#msgForm").attr("action"), data, function () {
            $("#MailDlg").dialog("close");
        });
    }

    function _replyMsg(id){
       var $msg=$("#msg_"+id);
       $("#txtSubject").val($msg.data("subject"));
       $("#txtReceiver").val($msg.data("sender"));  
       $('#MailDlg').dialog("open");
       $("#txtContent").val("\n\t"+$msg.find(".d-msg-body").text()).focus();
    }

    function _delMsg(id){
       var $msg=$("#msg_"+id);
       $.post("@Url.Action("DeleteMsg", "Account", new { Area = "" })",{id:id},function(){
         $msg.remove();
       });
    }

    $(function () {
        $('#MailDlg').dialog({
            autoOpen: false,
            dialogClass: 'd-dialog',
            hide: 'scale',
            modal: true,
            resizable: false,
            show: 'scale',
            open: function () {
               window.setTimeout(function(){
                $("#txtContent").richtextbox("active");
                },500);
            },
            width: 510,
            buttons: {
                "@Html.Global("RES_SEND")": function () { _sendMsg(); },
                "@Html.Global("RES_CLOSE")": function () {
                    $("#MailDlg").dialog('close');
                }
            }
        });
    });
</script>
